Imports org.apache.fop
Imports org.apache.fop.apps
Imports org.apache.fop.tools
Imports org.xml.sax
Imports java.io
Imports System.Xml
Imports System.Xml.Xsl
Imports System.IO
Imports System.Text

Imports MIMS.WRS.Business
Imports MIMS.WRS.Entities

Public Class RenderService
    Public Shared Function CreateCData(ByVal doc As XmlDocument, ByVal data As String) As XmlCDataSection
        Dim cdata As XmlCDataSection = doc.CreateCDataSection(data)
        Return cdata
    End Function

    Public Shared Function GenerateBOXML(ByVal source As BulkOrderCollection, ByVal imageRoot As String, ByVal discount As Decimal, ByVal showFOB As Boolean) As XmlDocument
        Dim doc As XmlDocument = New XmlDocument
        Dim root As XmlNode = doc.CreateElement("bulkorders")

        Dim showFOBNode As XmlNode = doc.CreateElement("showfob")
        showFOBNode.InnerText = Convert.ToInt32(showFOB).ToString()
        root.AppendChild(showFOBNode)

        For Each bo As BulkOrder In source
            Dim boNode As XmlNode = doc.CreateElement("bulkorder")
            Dim node As XmlNode = doc.CreateElement("productimagepath")
            Dim imagePath As String = Path.Combine(imageRoot, bo.ProductCode + ".jpg")
            If System.IO.File.Exists(imagePath) Then
                node.InnerText = imagePath
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("productcode")
            node.InnerText = bo.ProductCode
            boNode.AppendChild(node)

            node = doc.CreateElement("color")
            node.InnerText = bo.Color
            boNode.AppendChild(node)

            node = doc.CreateElement("salesdescription1")
            node.AppendChild(CreateCData(doc, bo.SalesDescription1))
            boNode.AppendChild(node)

            node = doc.CreateElement("salesdescription2")
            node.AppendChild(CreateCData(doc, bo.SalesDescription2))
            boNode.AppendChild(node)

            node = doc.CreateElement("isspecialoffer")
            If (bo.SpecialOfferDisc = Nulls.NullDecimal OrElse bo.SpecialOfferDisc = 0) Then
                node.InnerText = "0"
            Else
                node.InnerText = "1"
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("fob")
            If bo.FOB <> Nulls.NullDecimal Then
                If (bo.SpecialOfferDisc = Nulls.NullDecimal OrElse bo.SpecialOfferDisc = 0) Then
                    node.InnerText = String.Format("{0} (USD) per {1}", (bo.FOB * (1 - discount / 100)).ToString("#######.00"), _
                                            BulkOrder.GetBOPackTypeMeaning(bo.Pack))
                Else
                    Dim totalDiscount As Decimal = discount + bo.SpecialOfferDisc
                    node.InnerText = String.Format("{0} (USD) per {1}, SPECIAL PRICE,{2}% Discount included.", (bo.FOB * (1 - totalDiscount / 100)).ToString("#######.00"), _
                                                                BulkOrder.GetBOPackTypeMeaning(bo.Pack), totalDiscount.ToString("######"))
                End If
            End If
            boNode.AppendChild(node)

            'node = doc.CreateElement("fob2")
            'node.InnerText = "SPECIAL PRICE"
            'boNode.AppendChild(node)

            node = doc.CreateElement("owner")
            If (bo.Owner <> Nulls.NullInteger) Then
                Dim officeID As Integer = bo.Owner
                Try
                    node.InnerText = OfficeService.GetOffice(officeID).OfficeName
                Catch ex As Exception
                End Try
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("bono")
            node.InnerText = bo.BONo
            boNode.AppendChild(node)

            node = doc.CreateElement("suppliercode")
            node.InnerText = bo.SupplierCode
            boNode.AppendChild(node)

            node = doc.CreateElement("pack")
            node.InnerText = BulkOrder.GetBOPackTypeMeaning(bo.Pack)
            boNode.AppendChild(node)

            node = doc.CreateElement("type")
            node.InnerText = Utilities.GetBOTypeMeaning(CType(bo.Type, EBOType))
            boNode.AppendChild(node)

            node = doc.CreateElement("description")
            node.AppendChild(CreateCData(doc, bo.Description))
            boNode.AppendChild(node)

            node = doc.CreateElement("qtyavail")
            If (bo.QuantityAvailable <> Nulls.NullInteger) Then
                node.InnerText = bo.QuantityAvailable.ToString()
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("qtyavailcatalog")
            If (bo.QuantityAvailable <> Nulls.NullInteger AndAlso bo.PackQty <> Nulls.NullInteger) Then
                node.InnerText = BulkOrder.GetQuantityAvailableCatalog(bo.QuantityAvailable, bo.PackQty).ToString()
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("minorderqty")
            node.InnerText = bo.MinOrderQty
            'If (bo.MinOrderQty <> Nulls.NullInteger) Then
            '    node.InnerText = String.Format("{0} {1}", bo.MinOrderQty.ToString(), BulkOrder.GetBOPackTypeMeaning(bo.Pack))
            'End If
            boNode.AppendChild(node)

            node = doc.CreateElement("tallpalletqty")
            node.InnerText = bo.TallPaletQty
            boNode.AppendChild(node)

            node = doc.CreateElement("suppliercode")
            node.InnerText = bo.SupplierCode
            boNode.AppendChild(node)

            node = doc.CreateElement("catalogname")
            If (bo.Page.Trim().Length > 0) Then
                node.InnerText = bo.CatalogName.ToUpper()
            Else
                bo.CatalogName = "[un-used because nothing in page]"
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("page")
            node.InnerText = bo.Page
            boNode.AppendChild(node)

            node = doc.CreateElement("idsqtyavail")
            If (bo.IDSQuantityAvail <> Nulls.NullInteger) Then
                node.InnerText = bo.IDSQuantityAvail.ToString()
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("idsqtyavaildesc")
            If (bo.IDSQuantityAvail <> Nulls.NullInteger) Then
                node.InnerText = bo.IDSQuantityAvailDesc
            End If
            boNode.AppendChild(node)


            node = doc.CreateElement("homedelivered15price")
            If (bo.HomeDelivered15Price <> Nulls.NullDecimal) Then
                node.InnerText = bo.HomeDelivered15Price.ToString("#######.00")
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("currencycode")
            node.InnerText = bo.CurrencyCode
            boNode.AppendChild(node)

            node = doc.CreateElement("typecode")
            node.InnerText = bo.TypeCode
            boNode.AppendChild(node)

            node = doc.CreateElement("ownercode")
            node.InnerText = bo.OwnerCode
            boNode.AppendChild(node)

            node = doc.CreateElement("idssort")
            node.InnerText = bo.IDSSort.ToUpper().Trim()
            boNode.AppendChild(node)

            node = doc.CreateElement("stocklocation")
            node.InnerText = bo.StockLocation
            boNode.AppendChild(node)

            root.AppendChild(boNode)
        Next
        doc.AppendChild(root)
        Return doc
    End Function

#Region " Product Dimensions"

    Public Shared Function GetUnit(ByVal measurementUnit As String) As String
        Dim measuredUnit As String = "(inches)"
        If (measurementUnit = "Metric") Then
            measuredUnit = "(cm)"
        Else
            measuredUnit = "(inch)"
        End If
        If ((Not RequestContext.CurrentUser Is Nothing AndAlso RequestContext.CurrentUser.IS_ADMIN)) Then
            measuredUnit = "(cm)"
        End If
        Return measuredUnit
    End Function

    Private Shared Function DecValue(ByVal measuredUnit As String, ByVal value As Decimal) As String
        If (value = Nulls.NullDecimal) Then
            Return ""
        Else
            If (value = 0.0) Then
                Return ""
            End If
            If (measuredUnit = "(inches)") Then
                value = value / Convert.ToDecimal(2.54)
                If (value < 1) Then
                    Return "0" + value.ToString(Config.DecimalFormatEx)
                End If
            End If
            Return value.ToString(Config.DecimalFormatEx)
        End If
    End Function

    Public Shared Function GetTextByProductRound(ByVal bo As BulkOrderEx) As String
        If (Not bo.IsProductRound AndAlso bo.DimensionID > 0) Then
            Return "Length"
        End If
        Return "Dia"
    End Function

    Public Shared Function GetUnitWeightReturnedIsProductRound(ByVal measUnit As String, ByVal bo As BulkOrderEx, ByRef sbTextForTitleRow As StringBuilder, ByRef sbTextForDiaOrLengthRow As StringBuilder, ByVal sbTextForWidthRow As StringBuilder, ByRef sbTextForHeightRow As StringBuilder) As Boolean
        Dim measurementUnit As String = GetUnit(measUnit)
        Dim bWidth As Boolean = (Not bo.IsProductRound AndAlso bo.DimensionID > 0)

        sbTextForDiaOrLengthRow.AppendFormat("{0}{1}   ", GetTextByProductRound(bo), measurementUnit)
        If (bWidth) Then
            sbTextForWidthRow.AppendFormat("Width{0}", measurementUnit)
        End If
        sbTextForHeightRow.AppendFormat("Height{0}", measurementUnit)

        Dim bHasValue As Boolean = False
        If Not (bo.DiaOrLenS1 = Nulls.NullDecimal OrElse bo.DiaOrLenS1 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.HeightS1 = Nulls.NullDecimal OrElse bo.HeightS1 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.WidthS1 = Nulls.NullDecimal OrElse bo.WidthS1 = 0.0) Then
            bHasValue = True
        End If
        If (bHasValue) Then
            sbTextForTitleRow.AppendFormat("S1")
            sbTextForDiaOrLengthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.DiaOrLenS1))
            sbTextForWidthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.WidthS1))
            sbTextForHeightRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.HeightS1))
            bHasValue = False
        End If
        '-----------------------------------------------
        If Not (bo.DiaOrLenS2 = Nulls.NullDecimal OrElse bo.DiaOrLenS2 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.HeightS2 = Nulls.NullDecimal OrElse bo.HeightS2 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.WidthS2 = Nulls.NullDecimal OrElse bo.WidthS2 = 0.0) Then
            bHasValue = True
        End If
        If (bHasValue) Then
            sbTextForTitleRow.AppendFormat("          S2")
            sbTextForDiaOrLengthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.DiaOrLenS2))
            sbTextForWidthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.WidthS2))
            sbTextForHeightRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.HeightS2))
            bHasValue = False
        End If
        '-----------------------------------------------
        If Not (bo.DiaOrLenS3 = Nulls.NullDecimal OrElse bo.DiaOrLenS3 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.HeightS3 = Nulls.NullDecimal OrElse bo.HeightS3 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.WidthS3 = Nulls.NullDecimal OrElse bo.WidthS3 = 0.0) Then
            bHasValue = True
        End If
        If (bHasValue) Then
            sbTextForTitleRow.AppendFormat("          S3")
            sbTextForDiaOrLengthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.DiaOrLenS3))
            sbTextForWidthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.WidthS3))
            sbTextForHeightRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.HeightS3))
            bHasValue = False
        End If
        '-----------------------------------------------
        If Not (bo.DiaOrLenS4 = Nulls.NullDecimal OrElse bo.DiaOrLenS4 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.HeightS4 = Nulls.NullDecimal OrElse bo.HeightS4 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.WidthS4 = Nulls.NullDecimal OrElse bo.WidthS4 = 0.0) Then
            bHasValue = True
        End If
        If (bHasValue) Then
            sbTextForTitleRow.AppendFormat("          S4")
            sbTextForDiaOrLengthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.DiaOrLenS4))
            sbTextForWidthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.WidthS4))
            sbTextForHeightRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.HeightS4))
            bHasValue = False
        End If
        '-----------------------------------------------
        If Not (bo.DiaOrLenS5 = Nulls.NullDecimal OrElse bo.DiaOrLenS5 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.HeightS5 = Nulls.NullDecimal OrElse bo.HeightS5 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.WidthS5 = Nulls.NullDecimal OrElse bo.WidthS5 = 0.0) Then
            bHasValue = True
        End If
        If (bHasValue) Then
            sbTextForTitleRow.AppendFormat("          S5")
            sbTextForDiaOrLengthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.DiaOrLenS5))
            sbTextForWidthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.WidthS5))
            sbTextForHeightRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.HeightS5))
            bHasValue = False
        End If
        '-----------------------------------------------
        If Not (bo.DiaOrLenS6 = Nulls.NullDecimal OrElse bo.DiaOrLenS6 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.HeightS6 = Nulls.NullDecimal OrElse bo.HeightS6 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.WidthS6 = Nulls.NullDecimal OrElse bo.WidthS6 = 0.0) Then
            bHasValue = True
        End If
        If (bHasValue) Then
            sbTextForTitleRow.AppendFormat("          S6")
            sbTextForDiaOrLengthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.DiaOrLenS6))
            sbTextForWidthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.WidthS6))
            sbTextForHeightRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.HeightS6))
            bHasValue = False
        End If
        '-----------------------------------------------
        If Not (bo.DiaOrLenS7 = Nulls.NullDecimal OrElse bo.DiaOrLenS7 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.HeightS7 = Nulls.NullDecimal OrElse bo.HeightS7 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.WidthS7 = Nulls.NullDecimal OrElse bo.WidthS7 = 0.0) Then
            bHasValue = True
        End If
        If (bHasValue) Then
            sbTextForTitleRow.AppendFormat("          S7")
            sbTextForDiaOrLengthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.DiaOrLenS7))
            sbTextForWidthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.WidthS7))
            sbTextForHeightRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.HeightS7))
            bHasValue = False
        End If
        '-----------------------------------------------
        If Not (bo.DiaOrLenS8 = Nulls.NullDecimal OrElse bo.DiaOrLenS8 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.HeightS8 = Nulls.NullDecimal OrElse bo.HeightS8 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.WidthS8 = Nulls.NullDecimal OrElse bo.WidthS8 = 0.0) Then
            bHasValue = True
        End If
        If (bHasValue) Then
            sbTextForTitleRow.AppendFormat("          S8")
            sbTextForDiaOrLengthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.DiaOrLenS8))
            sbTextForWidthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.WidthS8))
            sbTextForHeightRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.HeightS8))
            bHasValue = False
        End If
        Return (bWidth)
    End Function

    Public Shared Function GetUnitWeightReturnedIsProductRound(ByVal measUnit As String, ByVal bo As BulkOrderEx, ByRef sbTextForTitleRow As StringBuilder, ByRef s1 As String, ByRef s2 As String, ByRef s3 As String, ByRef s4 As String, ByRef s5 As String, ByRef s6 As String, ByRef s7 As String, ByRef s8 As String, ByRef s5678 As String, _
     ByRef s1DiaOrLen As String, ByRef s2DiaOrLen As String, ByRef s3DiaOrLen As String, ByRef s4DiaOrLen As String, ByRef s5DiaOrLen As String, ByRef s6DiaOrLen As String, ByRef s7DiaOrLen As String, ByRef s8DiaOrLen As String, ByRef s5678DiaOrLen As String, _
     ByRef s1Height As String, ByRef s2Height As String, ByRef s3Height As String, ByRef s4Height As String, ByRef s5Height As String, ByRef s6Height As String, ByRef s7Height As String, ByRef s8Height As String, ByRef s5678Height As String, _
     ByRef s1Width As String, ByRef s2Width As String, ByRef s3Width As String, ByRef s4Width As String, ByRef s5Width As String, ByRef s6Width As String, ByRef s7Width As String, ByRef s8Width As String, ByRef s5678Width As String, ByRef sbTextForDiaOrLengthRow As StringBuilder, ByVal sbTextForWidthRow As StringBuilder, ByRef sbTextForHeightRow As StringBuilder) As Boolean
        Dim measurementUnit As String = GetUnit(measUnit)
        Dim bWidth As Boolean = (Not bo.IsProductRound AndAlso bo.DimensionID > 0)

        sbTextForDiaOrLengthRow.AppendFormat("{0}   {1}:", GetTextByProductRound(bo), measurementUnit)
        If (bWidth) Then
            sbTextForWidthRow.AppendFormat("Width {0}:", measurementUnit)
        End If
        sbTextForHeightRow.AppendFormat("Height {0}:", measurementUnit)

        Dim bHasValue As Boolean = False
        If Not (bo.DiaOrLenS1 = Nulls.NullDecimal OrElse bo.DiaOrLenS1 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.HeightS1 = Nulls.NullDecimal OrElse bo.HeightS1 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.WidthS1 = Nulls.NullDecimal OrElse bo.WidthS1 = 0.0) Then
            bHasValue = True
        End If
        If (bHasValue) Then
            sbTextForTitleRow.AppendFormat("S1")
            s1 = "S1"
            s1DiaOrLen = DecValue(measurementUnit, bo.DiaOrLenS1)
            s1Height = DecValue(measurementUnit, bo.HeightS1)
            s1Width = DecValue(measurementUnit, bo.WidthS1)
            'sbTextForDiaOrLengthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.DiaOrLenS1))
            'sbTextForWidthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.WidthS1))
            'sbTextForHeightRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.HeightS1))
            bHasValue = False
        End If
        '-----------------------------------------------
        If Not (bo.DiaOrLenS2 = Nulls.NullDecimal OrElse bo.DiaOrLenS2 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.HeightS2 = Nulls.NullDecimal OrElse bo.HeightS2 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.WidthS2 = Nulls.NullDecimal OrElse bo.WidthS2 = 0.0) Then
            bHasValue = True
        End If
        If (bHasValue) Then
            sbTextForTitleRow.AppendFormat("   S2")
            s2 = "S2"
            s2DiaOrLen = DecValue(measurementUnit, bo.DiaOrLenS2)
            s2Height = DecValue(measurementUnit, bo.HeightS2)
            s2Width = DecValue(measurementUnit, bo.WidthS2)
            'sbTextForDiaOrLengthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.DiaOrLenS2))
            'sbTextForWidthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.WidthS2))
            'sbTextForHeightRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.HeightS2))
            bHasValue = False
        End If
        '-----------------------------------------------
        If Not (bo.DiaOrLenS3 = Nulls.NullDecimal OrElse bo.DiaOrLenS3 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.HeightS3 = Nulls.NullDecimal OrElse bo.HeightS3 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.WidthS3 = Nulls.NullDecimal OrElse bo.WidthS3 = 0.0) Then
            bHasValue = True
        End If
        If (bHasValue) Then
            sbTextForTitleRow.AppendFormat("   S3")
            s3 = "S3"
            s3DiaOrLen = DecValue(measurementUnit, bo.DiaOrLenS3)
            s3Height = DecValue(measurementUnit, bo.HeightS3)
            s3Width = DecValue(measurementUnit, bo.WidthS3)
            'sbTextForDiaOrLengthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.DiaOrLenS3))
            'sbTextForWidthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.WidthS3))
            'sbTextForHeightRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.HeightS3))
            bHasValue = False
        End If
        '-----------------------------------------------
        If Not (bo.DiaOrLenS4 = Nulls.NullDecimal OrElse bo.DiaOrLenS4 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.HeightS4 = Nulls.NullDecimal OrElse bo.HeightS4 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.WidthS4 = Nulls.NullDecimal OrElse bo.WidthS4 = 0.0) Then
            bHasValue = True
        End If
        If (bHasValue) Then
            sbTextForTitleRow.AppendFormat("   S4")
            s4 = "S4"
            s4DiaOrLen = DecValue(measurementUnit, bo.DiaOrLenS4)
            s4Height = DecValue(measurementUnit, bo.HeightS4)
            s4Width = DecValue(measurementUnit, bo.WidthS4)
            'sbTextForDiaOrLengthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.DiaOrLenS4))
            'sbTextForWidthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.WidthS4))
            'sbTextForHeightRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.HeightS4))
            bHasValue = False
        End If
        '-----------------------------------------------
        If Not (bo.DiaOrLenS5 = Nulls.NullDecimal OrElse bo.DiaOrLenS5 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.HeightS5 = Nulls.NullDecimal OrElse bo.HeightS5 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.WidthS5 = Nulls.NullDecimal OrElse bo.WidthS5 = 0.0) Then
            bHasValue = True
        End If
        If (bHasValue) Then
            sbTextForTitleRow.AppendFormat("   S5")
            s5 = "S5"
            s5DiaOrLen = DecValue(measurementUnit, bo.DiaOrLenS5)
            s5Height = DecValue(measurementUnit, bo.HeightS5)
            s5Width = DecValue(measurementUnit, bo.WidthS5)

            s5678 = "S5"
            s5678DiaOrLen = DecValue(measurementUnit, bo.DiaOrLenS5)
            s5678Height = DecValue(measurementUnit, bo.HeightS5)
            s5678Width = DecValue(measurementUnit, bo.WidthS5)
            'sbTextForDiaOrLengthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.DiaOrLenS5))
            'sbTextForWidthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.WidthS5))
            'sbTextForHeightRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.HeightS5))
            bHasValue = False
        End If
        '-----------------------------------------------
        If Not (bo.DiaOrLenS6 = Nulls.NullDecimal OrElse bo.DiaOrLenS6 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.HeightS6 = Nulls.NullDecimal OrElse bo.HeightS6 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.WidthS6 = Nulls.NullDecimal OrElse bo.WidthS6 = 0.0) Then
            bHasValue = True
        End If
        If (bHasValue) Then
            sbTextForTitleRow.AppendFormat("   S6")
            s6 = "S6"
            s6DiaOrLen = DecValue(measurementUnit, bo.DiaOrLenS6)
            s6Height = DecValue(measurementUnit, bo.HeightS6)
            s6Width = DecValue(measurementUnit, bo.WidthS6)

            s5678 += " S6"
            s5678DiaOrLen += " " + DecValue(measurementUnit, bo.DiaOrLenS6)
            s5678Height += " " + DecValue(measurementUnit, bo.HeightS6)
            s5678Width += " " + DecValue(measurementUnit, bo.WidthS6)
            'sbTextForDiaOrLengthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.DiaOrLenS6))
            'sbTextForWidthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.WidthS6))
            'sbTextForHeightRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.HeightS6))
            bHasValue = False
        End If
        '-----------------------------------------------
        If Not (bo.DiaOrLenS7 = Nulls.NullDecimal OrElse bo.DiaOrLenS7 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.HeightS7 = Nulls.NullDecimal OrElse bo.HeightS7 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.WidthS7 = Nulls.NullDecimal OrElse bo.WidthS7 = 0.0) Then
            bHasValue = True
        End If
        If (bHasValue) Then
            sbTextForTitleRow.AppendFormat("   S7")
            s7 = "S7"
            s7DiaOrLen = DecValue(measurementUnit, bo.DiaOrLenS7)
            s7Height = DecValue(measurementUnit, bo.HeightS7)
            s7Width = DecValue(measurementUnit, bo.WidthS7)

            s5678 += " S7"
            s5678DiaOrLen += " " + DecValue(measurementUnit, bo.DiaOrLenS7)
            s5678Height += " " + DecValue(measurementUnit, bo.HeightS7)
            s5678Width += " " + DecValue(measurementUnit, bo.WidthS7)
            'sbTextForDiaOrLengthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.DiaOrLenS7))
            'sbTextForWidthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.WidthS7))
            'sbTextForHeightRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.HeightS7))
            bHasValue = False
        End If
        '-----------------------------------------------
        If Not (bo.DiaOrLenS8 = Nulls.NullDecimal OrElse bo.DiaOrLenS8 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.HeightS8 = Nulls.NullDecimal OrElse bo.HeightS8 = 0.0) Then
            bHasValue = True
        End If
        If Not (bo.WidthS8 = Nulls.NullDecimal OrElse bo.WidthS8 = 0.0) Then
            bHasValue = True
        End If
        If (bHasValue) Then
            sbTextForTitleRow.AppendFormat("   S8")
            s8 = "S8"
            s8DiaOrLen = DecValue(measurementUnit, bo.DiaOrLenS8)
            s8Height = DecValue(measurementUnit, bo.HeightS8)
            s8Width = DecValue(measurementUnit, bo.WidthS8)

            s5678 += " S8"
            s5678DiaOrLen += " " + DecValue(measurementUnit, bo.DiaOrLenS8)
            s5678Height += " " + DecValue(measurementUnit, bo.HeightS8)
            s5678Width += " " + DecValue(measurementUnit, bo.WidthS8)
            'sbTextForDiaOrLengthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.DiaOrLenS8))
            'sbTextForWidthRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.WidthS8))
            'sbTextForHeightRow.AppendFormat("  {0}", DecValue(measurementUnit, bo.HeightS8))
            bHasValue = False
        End If
        Return (Not bWidth)
    End Function


#End Region

    Public Shared Function GenerateBOXMLEx(ByVal source As BulkOrderExCollection, ByVal imageRoot As String, ByVal discount As Decimal, ByVal showFOB As Boolean, ByVal measurementUnit As String) As XmlDocument
        Dim sbTextForTitleRow As New System.Text.StringBuilder
        Dim sbTextForDiaOrLengthRow As New System.Text.StringBuilder
        Dim sbTextForWidthRow As New System.Text.StringBuilder
        Dim sbTextForHeightRow As New System.Text.StringBuilder
        Dim isProductRound As Boolean

        Dim s1, s2, s3, s4, s5, s6, s7, s8, s5678 As String
        Dim s1DiaOrLen, s2DiaOrLen, s3DiaOrLen, s4DiaOrLen, s5DiaOrLen, s6DiaOrLen, s7DiaOrLen, s8DiaOrLen, s5678DiaOrLen As String
        Dim s1Height, s2Height, s3Height, s4Height, s5Height, s6Height, s7Height, s8Height, s5678Height As String
        Dim s1Width, s2Width, s3Width, s4Width, s5Width, s6Width, s7Width, s8Width, s5678Width As String

        Dim doc As XmlDocument = New XmlDocument
        Dim root As XmlNode = doc.CreateElement("bulkorders")

        Dim showFOBNode As XmlNode = doc.CreateElement("showfob")
        showFOBNode.InnerText = Convert.ToInt32(showFOB).ToString()
        root.AppendChild(showFOBNode)

        For Each bo As BulkOrderEx In source
            Dim boNode As XmlNode = doc.CreateElement("bulkorder")
            Dim node As XmlNode = doc.CreateElement("productimagepath")
            Dim imagePath As String = Path.Combine(imageRoot, bo.ProductCode + ".jpg")
            If System.IO.File.Exists(imagePath) Then
                node.InnerText = imagePath
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("productcode")
            node.InnerText = bo.ProductCode
            boNode.AppendChild(node)

            node = doc.CreateElement("color")
            node.InnerText = bo.Color
            boNode.AppendChild(node)

            node = doc.CreateElement("salesdescription1")
            node.AppendChild(CreateCData(doc, bo.SalesDescription1))
            boNode.AppendChild(node)

            node = doc.CreateElement("salesdescription2")
            node.AppendChild(CreateCData(doc, bo.SalesDescription2))
            boNode.AppendChild(node)

            node = doc.CreateElement("isspecialoffer")
            If (bo.SpecialOfferDisc = Nulls.NullDecimal OrElse bo.SpecialOfferDisc = 0) Then
                node.InnerText = "0"
            Else
                node.InnerText = "1"
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("fob")
            If bo.UnitFOBPrice <> Nulls.NullDecimal Then
                Dim unitFOBPrice As String = ""
                Dim totalDiscount As Decimal
                If (bo.UnitFOBPrice = 0D) Then
                    unitFOBPrice = "0.00"
                ElseIf (bo.SpecialOfferDisc = Nulls.NullDecimal OrElse bo.SpecialOfferDisc = 0) Then
                    unitFOBPrice = (bo.UnitFOBPrice * (1 - discount / 100)).ToString("#######.00")
                Else
                    totalDiscount = discount + bo.SpecialOfferDisc
                    unitFOBPrice = (bo.UnitFOBPrice * (1 - totalDiscount / 100)).ToString("#######.00")
                End If

                If (bo.SpecialOfferDisc = Nulls.NullDecimal OrElse bo.SpecialOfferDisc = 0) Then
                    node.InnerText = String.Format("{0} (USD) per {1}", unitFOBPrice, _
                                            BulkOrder.GetBOPackTypeMeaning(bo.Pack))
                Else
                    node.InnerText = String.Format("{0} (USD) per {1}, SPECIAL PRICE,{2}% Discount included.", unitFOBPrice, _
                                                                BulkOrder.GetBOPackTypeMeaning(bo.Pack), totalDiscount.ToString("######"))
                End If
            End If
            boNode.AppendChild(node)

            'node = doc.CreateElement("fob2")
            'node.InnerText = "SPECIAL PRICE"
            'boNode.AppendChild(node)

            node = doc.CreateElement("owner")
            If (bo.Owner <> Nulls.NullInteger) Then
                Dim officeID As Integer = bo.Owner
                Try
                    node.InnerText = OfficeService.GetOffice(officeID).OfficeName
                Catch ex As Exception
                End Try
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("bono")
            node.InnerText = bo.BONo
            boNode.AppendChild(node)

            node = doc.CreateElement("suppliercode")
            node.InnerText = bo.SupplierCode
            boNode.AppendChild(node)

            node = doc.CreateElement("pack")
            node.InnerText = BulkOrder.GetBOPackTypeMeaning(bo.Pack)
            boNode.AppendChild(node)

            node = doc.CreateElement("type")
            node.InnerText = Utilities.GetBOTypeMeaning(CType(bo.Type, EBOType))
            boNode.AppendChild(node)

            node = doc.CreateElement("description")
            node.AppendChild(CreateCData(doc, bo.Description))
            boNode.AppendChild(node)

            node = doc.CreateElement("qtyavail")
            If (bo.QuantityAvailable <> Nulls.NullInteger) Then
                node.InnerText = bo.QuantityAvailable.ToString()
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("qtyavailcatalog")
            If (bo.QuantityAvailable <> Nulls.NullInteger AndAlso bo.PackQty <> Nulls.NullInteger) Then
                node.InnerText = BulkOrder.GetQuantityAvailableCatalog(bo.QuantityAvailable, bo.PackQty).ToString()
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("minorderqty")
            Dim minOrderQtyValue As String = ""
            If (bo.MinOrderQtyValue <> Nulls.NullInteger AndAlso bo.MinOrderQtyValue <> 0) Then
                minOrderQtyValue = bo.MinOrderQtyValue.ToString() & " "
            End If
            node.InnerText = minOrderQtyValue & bo.MinOrderQty
            'If (bo.MinOrderQty <> Nulls.NullInteger) Then
            '    node.InnerText = String.Format("{0} {1}", bo.MinOrderQty.ToString(), BulkOrder.GetBOPackTypeMeaning(bo.Pack))
            'End If
            boNode.AppendChild(node)

            node = doc.CreateElement("tallpalletqty")
            node.InnerText = bo.TallPaletQty
            boNode.AppendChild(node)

            node = doc.CreateElement("suppliercode")
            node.InnerText = bo.SupplierCode
            boNode.AppendChild(node)

            node = doc.CreateElement("catalogname")
            If (bo.Page.Trim().Length > 0) Then
                node.InnerText = bo.CatalogName.ToUpper()
            Else
                bo.CatalogName = "[un-used because nothing in page]"
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("page")
            node.InnerText = bo.Page
            boNode.AppendChild(node)

            node = doc.CreateElement("idsqtyavail")
            If (bo.IDSQuantityAvail <> Nulls.NullInteger) Then
                node.InnerText = bo.IDSQuantityAvail.ToString()
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("idsqtyavaildesc")
            If (bo.IDSQuantityAvail <> Nulls.NullInteger) Then
                node.InnerText = bo.IDSQuantityAvailDesc
            End If
            boNode.AppendChild(node)


            node = doc.CreateElement("homedelivered15price")
            If (bo.HomeDelivered15Price <> Nulls.NullDecimal) Then
                node.InnerText = bo.HomeDelivered15Price.ToString("#######.00")
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("currencycode")
            node.InnerText = bo.CurrencyCode
            boNode.AppendChild(node)

            node = doc.CreateElement("typecode")
            node.InnerText = bo.TypeCode
            boNode.AppendChild(node)

            node = doc.CreateElement("ownercode")
            node.InnerText = bo.OwnerCode
            boNode.AppendChild(node)

            node = doc.CreateElement("idssort")
            node.InnerText = bo.IDSSort.ToUpper().Trim()
            boNode.AppendChild(node)

            node = doc.CreateElement("stocklocation")
            node.InnerText = bo.StockLocation
            boNode.AppendChild(node)
            'for unit weight
            sbTextForTitleRow = New System.Text.StringBuilder
            sbTextForDiaOrLengthRow = New System.Text.StringBuilder
            sbTextForWidthRow = New System.Text.StringBuilder
            sbTextForHeightRow = New System.Text.StringBuilder
            s1 = ""
            s2 = ""
            s3 = ""
            s4 = ""
            s5 = ""
            s6 = ""
            s7 = ""
            s8 = ""
            s5678 = ""
            s1DiaOrLen = ""
            s2DiaOrLen = ""
            s3DiaOrLen = ""
            s4DiaOrLen = ""
            s5DiaOrLen = ""
            s6DiaOrLen = ""
            s7DiaOrLen = ""
            s8DiaOrLen = ""
            s5678DiaOrLen = ""
            s1Height = ""
            s2Height = ""
            s3Height = ""
            s4Height = ""
            s5Height = ""
            s6Height = ""
            s7Height = ""
            s8Height = ""
            s5678Height = ""
            s1Width = ""
            s2Width = ""
            s3Width = ""
            s4Width = ""
            s5Width = ""
            s6Width = ""
            s7Width = ""
            s8Width = ""
            s5678Width = ""

            'isProductRound = GetUnitWeightReturnedIsProductRound(measurementUnit, bo, sbTextForTitleRow, sbTextForDiaOrLengthRow, sbTextForWidthRow, sbTextForHeightRow)
            isProductRound = GetUnitWeightReturnedIsProductRound(measurementUnit, bo, sbTextForTitleRow, s1, s2, s3, s4, s5, s6, s7, s8, s5678, _
            s1DiaOrLen, s2DiaOrLen, s3DiaOrLen, s4DiaOrLen, s5DiaOrLen, s6DiaOrLen, s7DiaOrLen, s8DiaOrLen, s5678DiaOrLen, _
            s1Height, s2Height, s3Height, s4Height, s5Height, s6Height, s7Height, s8Height, s5678Height, _
            s1Width, s2Width, s3Width, s4Width, s5Width, s6Width, s7Width, s8Width, s5678Width, sbTextForDiaOrLengthRow, sbTextForWidthRow, sbTextForHeightRow)

            node = doc.CreateElement("isProductRound")
            node.InnerText = isProductRound.ToString().ToLower()
            boNode.AppendChild(node)

            node = doc.CreateElement("TextForTitleRow")
            node.InnerText = sbTextForTitleRow.ToString()
            boNode.AppendChild(node)

            node = doc.CreateElement("S1")
            node.InnerText = s1
            boNode.AppendChild(node)
            node = doc.CreateElement("S2")
            node.InnerText = s2
            boNode.AppendChild(node)
            node = doc.CreateElement("S3")
            node.InnerText = s3
            boNode.AppendChild(node)
            node = doc.CreateElement("S4")
            node.InnerText = s4
            boNode.AppendChild(node)
            node = doc.CreateElement("S5")
            node.InnerText = s5
            boNode.AppendChild(node)
            node = doc.CreateElement("S6")
            node.InnerText = s6
            boNode.AppendChild(node)
            node = doc.CreateElement("S7")
            node.InnerText = s7
            boNode.AppendChild(node)
            node = doc.CreateElement("S8")
            node.InnerText = s8
            boNode.AppendChild(node)
            node = doc.CreateElement("S5678")
            node.InnerText = s5678
            boNode.AppendChild(node)

            node = doc.CreateElement("S1DiaOrLen")
            node.InnerText = s1DiaOrLen
            boNode.AppendChild(node)
            node = doc.CreateElement("S2DiaOrLen")
            node.InnerText = s2DiaOrLen
            boNode.AppendChild(node)
            node = doc.CreateElement("S3DiaOrLen")
            node.InnerText = s3DiaOrLen
            boNode.AppendChild(node)
            node = doc.CreateElement("S4DiaOrLen")
            node.InnerText = s4DiaOrLen
            boNode.AppendChild(node)
            node = doc.CreateElement("S5DiaOrLen")
            node.InnerText = s5DiaOrLen
            boNode.AppendChild(node)
            node = doc.CreateElement("S6DiaOrLen")
            node.InnerText = s6DiaOrLen
            boNode.AppendChild(node)
            node = doc.CreateElement("S7DiaOrLen")
            node.InnerText = s7DiaOrLen
            boNode.AppendChild(node)
            node = doc.CreateElement("S8DiaOrLen")
            node.InnerText = s8DiaOrLen
            boNode.AppendChild(node)
            node = doc.CreateElement("S5678DiaOrLen")
            node.InnerText = s5678DiaOrLen
            boNode.AppendChild(node)
            '
            node = doc.CreateElement("S1Height")
            node.InnerText = s1Height
            boNode.AppendChild(node)
            node = doc.CreateElement("S2Height")
            node.InnerText = s2Height
            boNode.AppendChild(node)
            node = doc.CreateElement("S3Height")
            node.InnerText = s3Height
            boNode.AppendChild(node)
            node = doc.CreateElement("S4Height")
            node.InnerText = s4Height
            boNode.AppendChild(node)
            node = doc.CreateElement("S5Height")
            node.InnerText = s5Height
            boNode.AppendChild(node)
            node = doc.CreateElement("S6Height")
            node.InnerText = s6Height
            boNode.AppendChild(node)
            node = doc.CreateElement("S7Height")
            node.InnerText = s7Height
            boNode.AppendChild(node)
            node = doc.CreateElement("S8Height")
            node.InnerText = s8Height
            boNode.AppendChild(node)
            boNode.AppendChild(node)
            node = doc.CreateElement("S5678Height")
            node.InnerText = s5678Height
            boNode.AppendChild(node)

            node = doc.CreateElement("S1Width")
            node.InnerText = s1Width
            boNode.AppendChild(node)
            node = doc.CreateElement("S2Width")
            node.InnerText = s2Width
            boNode.AppendChild(node)
            node = doc.CreateElement("S3Width")
            node.InnerText = s3Width
            boNode.AppendChild(node)
            node = doc.CreateElement("S4Width")
            node.InnerText = s4Width
            boNode.AppendChild(node)
            node = doc.CreateElement("S5Width")
            node.InnerText = s5Width
            boNode.AppendChild(node)
            node = doc.CreateElement("S6Width")
            node.InnerText = s6Width
            boNode.AppendChild(node)
            node = doc.CreateElement("S7Width")
            node.InnerText = s7Width
            boNode.AppendChild(node)
            node = doc.CreateElement("S8Width")
            node.InnerText = s8Width
            boNode.AppendChild(node)
            boNode.AppendChild(node)
            node = doc.CreateElement("S5678Width")
            node.InnerText = s5678Width
            boNode.AppendChild(node)

            node = doc.CreateElement("TextForDiaOrLengthRow")
            node.InnerText = sbTextForDiaOrLengthRow.ToString()
            boNode.AppendChild(node)

            node = doc.CreateElement("TextForWidthRow")
            node.InnerText = sbTextForWidthRow.ToString()
            boNode.AppendChild(node)

            node = doc.CreateElement("TextForHeightRow")
            node.InnerText = sbTextForHeightRow.ToString()
            boNode.AppendChild(node)

            root.AppendChild(boNode)
        Next
        doc.AppendChild(root)
        Return doc
    End Function

    Public Shared Function GenerateBOXML(ByVal source As IDataReader, ByVal imageRoot As String, ByVal discount As Decimal, ByVal showFOB As Boolean) As String
        Dim doc As XmlDocument = New XmlDocument
        Dim root As XmlNode = doc.CreateElement("bulkorders")

        Dim showFOBNode As XmlNode = doc.CreateElement("showfob")
        showFOBNode.InnerText = Convert.ToInt32(showFOB).ToString()
        root.AppendChild(showFOBNode)

        While source.Read()
            Dim boNode As XmlNode = doc.CreateElement("bulkorder")
            Dim node As XmlNode = doc.CreateElement("productimagepath")
            Dim imagePath As String = Path.Combine(imageRoot, CType(source("ProductCode"), String) + ".jpg")
            If System.IO.File.Exists(imagePath) Then
                node.InnerText = imagePath
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("productcode")
            If (source.IsDBNull(source.GetOrdinal("ProductCode")) = False) Then
                node.InnerText = CType(source("ProductCode"), String)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("color")
            If (source.IsDBNull(source.GetOrdinal("Color")) = False) Then
                node.InnerText = CType(source("Color"), String)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("salesdescription1")
            If (source.IsDBNull(source.GetOrdinal("salesdescription1")) = False) Then
                node.InnerText = CType(source("salesdescription1"), String)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("salesdescription2")
            If (source.IsDBNull(source.GetOrdinal("salesdescription2")) = False) Then
                node.InnerText = CType(source("salesdescription2"), String)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("fob")
            If (source.IsDBNull(source.GetOrdinal("FOB")) = False) Then
                node.InnerText = (CDec(source("FOB")) * (1 - discount / 100)).ToString("#######.00")
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("owner")
            If (source.IsDBNull(source.GetOrdinal("owner")) = False) Then
                Dim officeID As Integer = Convert.ToInt32(source("owner"))
                Try
                    node.InnerText = OfficeService.GetOffice(officeID).OfficeName
                Catch ex As Exception
                End Try
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("bono")
            If (source.IsDBNull(source.GetOrdinal("bono")) = False) Then
                node.InnerText = CType(source("bono"), String)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("suppliercode")
            If (source.IsDBNull(source.GetOrdinal("SupplierCode")) = False) Then
                node.InnerText = CType(source("SupplierCode"), String)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("type")
            If (source.IsDBNull(source.GetOrdinal("type")) = False) Then
                node.InnerText = Utilities.GetBOTypeMeaning(CType(source("type"), EBOType))
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("description")
            If (source.IsDBNull(source.GetOrdinal("description")) = False) Then
                node.InnerText = CType(source("description"), String)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("qtyordered")
            If (source.IsDBNull(source.GetOrdinal("TotalQtyOrdered")) = False) Then
                node.InnerText = CType(source("TotalQtyOrdered"), String)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("startdate")
            If (source.IsDBNull(source.GetOrdinal("StartDate")) = False) Then
                node.InnerText = CType(source("StartDate"), DateTime).ToString(Config.DateFormat)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("pack")
            If (source.IsDBNull(source.GetOrdinal("pack")) = False) Then
                node.InnerText = BulkOrder.GetBOPackTypeMeaning(Convert.ToString(source("pack")))
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("packqty")
            If (source.IsDBNull(source.GetOrdinal("packqty")) = False) Then
                node.InnerText = CType(source("packqty"), String)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("qtycompleted")
            If (source.IsDBNull(source.GetOrdinal("TotalQtyCompleted")) = False) Then
                node.InnerText = CType(source("TotalQtyCompleted"), String)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("qtybelow50")
            If (source.IsDBNull(source.GetOrdinal("TotalQtyInProgressBelow50P")) = False) Then
                node.InnerText = CType(source("TotalQtyInProgressBelow50P"), String)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("qtyabove50")
            If (source.IsDBNull(source.GetOrdinal("TotalQtyInProgressAbove50P")) = False) Then
                node.InnerText = CType(source("TotalQtyInProgressAbove50P"), String)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("qtyavail")
            If (source.IsDBNull(source.GetOrdinal("QuantityAvailable")) = False) Then
                node.InnerText = CType(source("QuantityAvailable"), String)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("qtyavailcatalog")
            If (source.IsDBNull(source.GetOrdinal("QuantityAvailable")) = False AndAlso source.IsDBNull(source.GetOrdinal("PackQty")) = False) Then
                node.InnerText = BulkOrder.GetQuantityAvailableCatalog(CType(source("QuantityAvailable"), Integer), CType(source("PackQty"), Integer)).ToString()
            End If
            boNode.AppendChild(node)

            'node = doc.CreateElement("qtyshipped")
            'If (source.IsDBNull(source.GetOrdinal("QtyShipped")) = False) Then
            '    node.InnerText = CType(source("QtyShipped"), String)
            'End If
            'boNode.AppendChild(node)

            'node = doc.CreateElement("qtynotshipped")
            'If (source.IsDBNull(source.GetOrdinal("QtyNotShipped")) = False) Then
            '    node.InnerText = CType(source("QtyNotShipped"), String)
            'End If
            'boNode.AppendChild(node)

            node = doc.CreateElement("actualstock")
            If (source.IsDBNull(source.GetOrdinal("ActualStock")) = False) Then
                node.InnerText = CType(source("ActualStock"), String)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("notstarted")
            If (source.IsDBNull(source.GetOrdinal("NotStarted")) = False) Then
                node.InnerText = CType(source("NotStarted"), String)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("estimateddate")
            If (source.IsDBNull(source.GetOrdinal("EstimatedDate")) = False) Then
                node.InnerText = CType(source("EstimatedDate"), DateTime).ToString(Config.DateFormat)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("stocklastcheckeddate")
            If (source.IsDBNull(source.GetOrdinal("StockLastCheckedDate")) = False) Then
                node.InnerText = CType(source("StockLastCheckedDate"), DateTime).ToString(Config.DateFormat)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("stocklastcheckedqty")
            If (source.IsDBNull(source.GetOrdinal("StockLastCheckedQty")) = False) Then
                node.InnerText = CType(source("StockLastCheckedQty"), String)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("minorderqty")
            If (source.IsDBNull(source.GetOrdinal("MinOrderQty")) = False) Then
                node.InnerText = String.Format("{0} {1}", CType(source("MinOrderQty"), String), BulkOrder.GetBOPackTypeMeaning(Convert.ToString(source("pack"))))
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("tallpalletqty")
            If (source.IsDBNull(source.GetOrdinal("TallPaletQty")) = False) Then
                node.InnerText = String.Format("{0} {1}", CType(source("TallPaletQty"), String), BulkOrder.GetBOPackTypeMeaning(Convert.ToString(source("pack"))))
            End If
            boNode.AppendChild(node)


            root.AppendChild(boNode)
        End While
        doc.AppendChild(root)
        Return doc.OuterXml
    End Function

    Public Shared Function GenerateFO(ByVal xml As String, ByVal xslFile As String, ByVal foFile As String) As Boolean
        Try
            Dim doc As XmlDocument = New XmlDocument
            doc.LoadXml(xml)
            doc.PreserveWhitespace = True

            ' an ugly fix for PreserveWhitespace in .NET XML
            doc.DocumentElement.PrependChild(doc.CreateTextNode(" "))

            Dim xslt As XslTransform = New XslTransform
            xslt.Load(xslFile)

            Dim writer As XmlTextWriter = New XmlTextWriter(foFile, Nothing)
            xslt.Transform(doc, Nothing, writer, Nothing)
            writer.Close()

            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function

    Public Shared Function RenderFOtoPDF(ByVal foFile As String) As Byte()
        Dim input As FileInputStream = New FileInputStream(foFile)
        Dim source As InputSource = New InputSource(input)
        Dim output As ByteArrayOutputStream = New ByteArrayOutputStream
        Dim driver As driver = New driver(source, output)
        driver.setRenderer(driver.RENDER_PDF)
        driver.run()
        ' copy output to .NET Stream                                
        Dim outBuffer As [SByte]() = output.toByteArray()
        Dim pdfBuffer() As Byte
        ReDim pdfBuffer(outBuffer.Length - 1)
        Buffer.BlockCopy(outBuffer, 0, pdfBuffer, 0, outBuffer.Length)
        output.close()
        Return pdfBuffer
    End Function

    Public Shared Function GenerateOrdersBOXML(ByVal source As IDataReader) As String
        Dim dVolume20, dVolume40, dVolume40HiCube, total, totalWeight As Double
        Dim roadWeight As Decimal = 0
        Dim measUnit As String

        Dim doc As XmlDocument = New XmlDocument
        Dim root As XmlNode = doc.CreateElement("bulkorders")
        Dim index As Integer = 0
        Dim countryId As Integer = 0
        While source.Read()
            Dim boNode As XmlNode = doc.CreateElement("bulkorder")

            countryId = 0
            If (source.IsDBNull(source.GetOrdinal("DestinationCountryId")) = False) Then
                countryId = CInt(source("DestinationCountryId"))
            End If
            InitializedValues(countryId, dVolume20, dVolume40, dVolume40HiCube, measUnit, roadWeight)

            'No
            index = index + 1
            Dim node As XmlNode = doc.CreateElement("No")
            node.InnerText = index.ToString()
            boNode.AppendChild(node)

            'OrderDate
            node = doc.CreateElement("OrderDate")
            If (source.IsDBNull(source.GetOrdinal("OrderDate")) = False) Then
                node.InnerText = CType(source("OrderDate"), DateTime).ToString(Config.DateFormat)
            End If
            boNode.AppendChild(node)

            'CompanyName
            node = doc.CreateElement("CompanyName")
            If (source.IsDBNull(source.GetOrdinal("CompanyName")) = False) Then
                node.InnerText = CType(source("CompanyName"), String)
            End If
            boNode.AppendChild(node)

            'CustomerName
            node = doc.CreateElement("CustomerName")
            Dim customerName As String = ""
            If (source.IsDBNull(source.GetOrdinal("Title")) = False) Then
                customerName = CType(source("Title"), String)
            End If
            Dim tempName As String = ""
            If (source.IsDBNull(source.GetOrdinal("FamilyName")) = False) Then
                tempName = CType(source("FamilyName"), String).Trim()
                customerName = Convert.ToString(IIf(customerName = "", tempName, customerName & " " & tempName))
            End If
            If (source.IsDBNull(source.GetOrdinal("GivenName")) = False) Then
                tempName = CType(source("GivenName"), String).Trim()
                customerName = Convert.ToString(IIf(customerName = "", tempName, customerName & " " & tempName))
            End If
            node.InnerText = customerName
            boNode.AppendChild(node)

            'StreetAddress
            node = doc.CreateElement("StreetAddress")
            If (source.IsDBNull(source.GetOrdinal("StreetAddress")) = False) Then
                node.InnerText = CType(source("StreetAddress"), String)
            End If
            boNode.AppendChild(node)

            'GetAddressInfo
            Dim addressInfo As String = ""
            node = doc.CreateElement("AddressInfo")
            tempName = ""
            If (source.IsDBNull(source.GetOrdinal("City")) = False) Then
                addressInfo = CType(source("City"), String)
            End If
            If (source.IsDBNull(source.GetOrdinal("State")) = False) Then
                tempName = CType(source("State"), String)
                If (tempName.Trim() <> "") Then
                    addressInfo = Convert.ToString(IIf(addressInfo = "", tempName, addressInfo & ", " & tempName))
                End If
            End If
            If (source.IsDBNull(source.GetOrdinal("ZipCode")) = False) Then
                tempName = CType(source("ZipCode"), String)
                If (tempName.Trim() <> "") Then
                    addressInfo = Convert.ToString(IIf(addressInfo = "", tempName, addressInfo & ", " & tempName))
                End If
            End If
            If (source.IsDBNull(source.GetOrdinal("Country")) = False) Then
                tempName = CType(source("Country"), String)
                If (tempName.Trim() <> "") Then
                    addressInfo = Convert.ToString(IIf(addressInfo = "", tempName, addressInfo & ", " & tempName))
                End If
            End If
            node.InnerText = addressInfo
            boNode.AppendChild(node)

            'EmailAddress
            node = doc.CreateElement("EmailAddress")
            If (source.IsDBNull(source.GetOrdinal("EmailAddress")) = False) Then
                node.InnerText = CType(source("EmailAddress"), String)
            End If
            boNode.AppendChild(node)

            Dim totalVolume As Double = 0
            totalWeight = 0
            'GetQty20Text
            node = doc.CreateElement("Qty20")
            tempName = "0"
            Dim qty As Integer = 0
            If (source.IsDBNull(source.GetOrdinal("Qty20")) = False) Then
                qty = CInt(source("Qty20"))
                If (qty <> 0) Then
                    tempName = qty.ToString(Config.NumericFormat)
                End If
            End If
            totalVolume = (qty * dVolume20)
            totalWeight = (qty * roadWeight)
            node.InnerText = tempName & " x 20' = " & (qty * dVolume20).ToString & " " & measUnit
            boNode.AppendChild(node)

            'GetQty40Text
            node = doc.CreateElement("Qty40")
            tempName = "0"
            qty = 0
            If (source.IsDBNull(source.GetOrdinal("Qty40")) = False) Then
                qty = CInt(source("Qty40"))
                If (qty <> 0) Then
                    tempName = qty.ToString(Config.NumericFormat)
                End If
            End If
            totalVolume = totalVolume + (qty * dVolume40)
            totalWeight = totalWeight + (qty * roadWeight)
            node.InnerText = tempName & " x 40' = " & (qty * dVolume40).ToString & " " & measUnit
            boNode.AppendChild(node)

            'GetQty40HCText
            node = doc.CreateElement("Qty40HC")
            tempName = "0"
            qty = 0
            If (source.IsDBNull(source.GetOrdinal("Qty40HC")) = False) Then
                qty = CInt(source("Qty40HC"))
                If (qty <> 0) Then
                    tempName = qty.ToString(Config.NumericFormat)
                End If
            End If
            totalVolume = totalVolume + (qty * dVolume40HiCube)
            totalWeight = totalWeight + (qty * roadWeight)
            node.InnerText = tempName & " x 40'HC = " & (qty * dVolume40HiCube).ToString & " " & measUnit
            boNode.AppendChild(node)

            'GetTotalVolumeText
            node = doc.CreateElement("TotalVolume")
            tempName = "0.00"
            If (totalVolume <> 0) Then
                tempName = totalVolume.ToString(Config.DecimalFormat2)
            End If
            node.InnerText = tempName & " " & measUnit
            boNode.AppendChild(node)

            'GetTotalWeightText
            tempName = "0.00"
            node = doc.CreateElement("TotalWeight")
            If (totalVolume <> 0) Then
                tempName = totalWeight.ToString(Config.DecimalFormat2)
            End If
            Dim weightUnit As String = CType(IIf(measUnit = "cu ft", "lbs", "kg"), String)
            node.InnerText = tempName & " " & weightUnit
            boNode.AppendChild(node)

            'OrderNo
            node = doc.CreateElement("OrderNo")
            If (source.IsDBNull(source.GetOrdinal("OrderNo")) = False) Then
                node.InnerText = CType(source("OrderNo"), String)
            End If
            boNode.AppendChild(node)

            'OrderNo
            node = doc.CreateElement("HalfPalletSpaces")
            If (source.IsDBNull(source.GetOrdinal("HalfPalletSpaces")) = False) Then
                qty = CInt(source("HalfPalletSpaces"))
                node.InnerText = qty.ToString(Config.NumericFormat)
            End If
            boNode.AppendChild(node)

            'FilledVolume
            node = doc.CreateElement("FilledVolume")
            tempName = "0.00"
            If (source.IsDBNull(source.GetOrdinal("FilledVolume")) = False) Then
                Dim filledVol As Decimal = CDec(source("FilledVolume"))
                If (filledVol <> 0) Then
                    tempName = filledVol.ToString(Config.DecimalFormat2)
                End If
            End If
            node.InnerText = tempName
            boNode.AppendChild(node)

            'OrderType
            node = doc.CreateElement("OrderType")
            If (source.IsDBNull(source.GetOrdinal("OrderType")) = False) Then
                node.InnerText = CType(source("OrderType"), String)
            End If
            boNode.AppendChild(node)

            root.AppendChild(boNode)
        End While
        doc.AppendChild(root)
        Return doc.OuterXml
    End Function

    Public Shared Function GenerateOrdersBOXML(ByVal source As IDataReader, ByVal strFOBorHDHeader As String, ByVal strCompanyName As String) As String
        Dim doc As XmlDocument = New XmlDocument
        Dim root As XmlNode = doc.CreateElement("bulkorders")

        Dim showFOBNode As XmlNode = doc.CreateElement("showfob")
        showFOBNode.InnerText = strFOBorHDHeader
        root.AppendChild(showFOBNode)

        Dim companyName As XmlNode = doc.CreateElement("companyName")
        companyName.InnerText = strCompanyName
        root.AppendChild(companyName)

        Dim index As Integer
        While source.Read()
            Dim boNode As XmlNode = doc.CreateElement("bulkorder")

            'No
            index = index + 1
            Dim node As XmlNode = doc.CreateElement("No")
            node.InnerText = index.ToString()
            boNode.AppendChild(node)

            node = doc.CreateElement("ProductCode")
            If (source.IsDBNull(source.GetOrdinal("ProductCode")) = False) Then
                node.InnerText = CType(source("ProductCode"), String)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("Description")
            If (source.IsDBNull(source.GetOrdinal("Description")) = False) Then
                node.InnerText = CType(source("Description"), String)
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("Qty")
            Dim qty As Integer = 0
            Dim tempName As String = ""
            If (source.IsDBNull(source.GetOrdinal("Qty")) = False) Then
                qty = CInt(source("Qty"))
                If (qty <> 0) Then
                    tempName = qty.ToString(Config.NumericFormat)
                End If
            End If
            boNode.AppendChild(node)

            node = doc.CreateElement("UnitSetting")
            If (source.IsDBNull(source.GetOrdinal("UnitSetting")) = False) Then
                node.InnerText = CType(source("UnitSetting"), String)
            End If
            boNode.AppendChild(node)

            Dim dbValue As Double
            node = doc.CreateElement("UnitPrice")
            tempName = ""
            If (source.IsDBNull(source.GetOrdinal("UnitPrice")) = False) Then
                dbValue = CDbl(source("UnitPrice"))
                If (dbValue <> 0) Then
                    tempName = dbValue.ToString(Config.DecimalFormat2())
                End If
            End If
            node.InnerText = tempName
            boNode.AppendChild(node)

            node = doc.CreateElement("TotalPrice")
            tempName = ""
            If (source.IsDBNull(source.GetOrdinal("TotalPrice")) = False) Then
                dbValue = CDbl(source("TotalPrice"))
                If (dbValue <> 0) Then
                    tempName = dbValue.ToString(Config.DecimalFormat2())
                End If
            End If
            node.InnerText = tempName
            boNode.AppendChild(node)


            node = doc.CreateElement("Notes")
            If (source.IsDBNull(source.GetOrdinal("Notes")) = False) Then
                node.InnerText = CType(source("Notes"), String)
            End If
            boNode.AppendChild(node)


            node = doc.CreateElement("PriorityIndex")
            tempName = ""
            If (source.IsDBNull(source.GetOrdinal("PriorityIndex")) = False) Then
                Select Case CInt(source("PriorityIndex"))
                    Case 1
                        tempName = "Increase"
                    Case 2
                        tempName = "Exact"
                    Case 3
                        tempName = "Reduce"
                End Select
            End If
            node.InnerText = tempName
            boNode.AppendChild(node)

            node = doc.CreateElement("UnitWeight")
            tempName = ""
            If (source.IsDBNull(source.GetOrdinal("UnitWeight")) = False) Then
                dbValue = CDbl(source("UnitWeight"))
                If (dbValue <> 0) Then
                    tempName = dbValue.ToString(Config.DecimalFormat2())
                End If
            End If
            node.InnerText = tempName
            boNode.AppendChild(node)

            node = doc.CreateElement("MinQtyVolume")
            tempName = ""
            If (source.IsDBNull(source.GetOrdinal("MinQtyVolume")) = False) Then
                dbValue = CDbl(source("MinQtyVolume"))
                If (dbValue <> 0) Then
                    tempName = dbValue.ToString(Config.DecimalFormat2())
                End If
            End If
            node.InnerText = tempName
            boNode.AppendChild(node)

            root.AppendChild(boNode)
        End While
        doc.AppendChild(root)
        Return doc.OuterXml
    End Function

    Private Const ONE_VOLUME_CBM_TO_CUFT As Double = 35.31
    Private Const ONE_KG_TO_LBS As Double = 2.204

    Private Shared Sub InitializedValues(ByVal countryId As Integer, ByRef dVolume20 As Double, ByRef dVolume40 As Double, ByRef dVolume40HiCube As Double, ByRef measUnit As String, ByRef roadWeight As Decimal)
        Dim total, totalWeight As Double

        Dim selectedCountry As Country
        Try
            selectedCountry = CountryService.GetCountry(countryId)
        Catch ex As Exception
        End Try
        If (Not selectedCountry Is Nothing) Then
            Select Case selectedCountry.CountryCode
                Case "USA"
                    measUnit = "cu ft"
                Case Else
                    measUnit = "cbm"
                    'Todo: review later
                    'Case "UK", "ES", "ITA", "NZ", "NO", "TKY"
                    '    hidMeasUnit.Value = "cbm"
                    'Case Else
                    '    hidMeasUnit.Value = "m3"
            End Select
            roadWeight = selectedCountry.RoadWeight
        End If
        total = 0
        totalWeight = 0
        If measUnit = "cu ft" Then
            dVolume20 = 35 * ONE_VOLUME_CBM_TO_CUFT
            dVolume40 = 70 * ONE_VOLUME_CBM_TO_CUFT
            dVolume40HiCube = 80 * ONE_VOLUME_CBM_TO_CUFT
        ElseIf (measUnit = "cbm") Then
            dVolume20 = GetCBMValueByUnitCode("20C")
            dVolume40 = GetCBMValueByUnitCode("40C")
            dVolume40HiCube = GetCBMValueByUnitCode("40HC")
        Else
            dVolume20 = 30
            dVolume40 = 60
            dVolume40HiCube = 76
        End If
    End Sub

    Public Shared Function GetCBMValueByUnitCode(ByVal unitCode As String) As Decimal
        Dim result As Decimal = 0
        Dim settingCollection As UnitSettingCollection = UnitSettingService.GetUnitSettings()

        If (Not settingCollection Is Nothing AndAlso unitCode > "") Then
            For i As Integer = 0 To settingCollection.Count - 1
                If (settingCollection(i).UnitCode = unitCode) Then
                    Return settingCollection(i).CbmValue
                End If
            Next
        End If
        Return result
    End Function

End Class
